package com.example.stack.common;

/**
 * 统一整个项目中请求返回的结果（未来也可以用于分布式架构各个模块间调用时返回统一类型）
 */
public class ResultEntity<T> {

    public static final String SUCCESS_CODE = "200";
    public static final String FAILED = "FAILED";

    // 用来封装当前请求处理的结果是成功还是失败
    private String code;

    // 请求处理失败时返回的错误消息
    private String message;

    // 要返回的数据
    private T data;

    @Override
    public String toString() {
        return "ResultEntity{" +
                "code='" + code + '\'' +
                ", message='" + message + '\'' +
                ", data=" + data +
                '}';
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public ResultEntity(String code, String message, T data) {
        this.code = code;
        this.message = message;
        this.data = data;
    }

    public ResultEntity() {}

    /**
     * 请求成功 不需要返回数据
     * @return
     */
    public static <E> ResultEntity<E> successWithoutData(String ...message) {
        return new ResultEntity<E>(SUCCESS_CODE, getMsg(message), null);
    }

    /**
     * 请求成功将数据返回
     */
    public static <E> ResultEntity<E> successWithData(E data, String ...message) {
        return new ResultEntity<E>(SUCCESS_CODE, getMsg(message), data);
    }

    /**
     * 请求处理失败后使用的工具方法
     * @param message 失败的错误消息
     * @return
     */
    public static <E> ResultEntity<E> failed(String ...message) {
        return new ResultEntity<E>(FAILED, getMsg(message), null);
    }

    private static String getMsg(String[] message){
        StringBuilder msg = null;
        if(null != message && message.length > 0){
            msg = new StringBuilder();
            for (String temp : message) {
                if(temp != null){
                    msg.append(temp);
                    if(!message[message.length-1].equals(temp)){
                        msg.append("\t");
                    }
                }
            }
        }
        return msg == null ? null : msg.toString();
    }
}
